/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.datawander.Presenter;

import com.googlecode.datawander.DataWanderPresenterMock;
import com.googlecode.datawander.DataWanderViewIntMock;
import com.googlecode.datawander.TestConfiguration;
import com.googlecode.datawander.View.ConnectionValues;
import com.googlecode.datawander.connectors.Db4oUtil;
import com.googlecode.datawander.connectors.MyRelationalDataBaseException;
import com.googlecode.datawander.connectors.OracleConnector;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Teplate to another test migration object to relational database.
 * @author Marcin Stachniuk
 */
public class Object2RelationalMigration {

    private String dir;
    private String databaseUser;
    private String objectDBFilename;

    /**
     * Constructor of test.
     * @param dir Dir to folder where is copiled class folder and object
     * database file. Here should be a part of directory - a name of folder
     * in <main_project_folder>/testCompiledCode/.
     * @param databaseUser Name of database user where shoudl be migrate data.
     * Database driver, password, and URL is constant in all tests:
     * driver = oracle.jdbc.driver.OracleDriver
     * password = 123
     * URL = jdbc:oracle:thin:@localhost:1521:xe
     * @param objectDBFilename Name of object database filename. This file
     * should be placed in <main_project_folder>/testCompiledCode/<dir>/ folder.
     * In this parametr should be only filename (over path).
     */
    public Object2RelationalMigration(String dir, String databaseUser, String objectDBFilename) {
        this.dir = dir;
        this.databaseUser = databaseUser;
        this.objectDBFilename = objectDBFilename;
    }

    public static void setUpClass() throws Exception {

        TestConfiguration.configureLog4j();

        TestConfiguration.configureI18n();
    }

    public static void tearDownClass() throws Exception {
    }

    public void setUp() throws MyRelationalDataBaseException, SQLException {
        dropAllTables();
    }

    public void tearDown() {
        Db4oUtil.closeObjectContainer();
        Db4oUtil.shutdown();
    }

    public void testMigrationProcess() throws Exception {
        System.out.println("Object2RelationalMigration.migrationProcess()");

        // given
        Object2RelationalMigrationThread instance = new Object2RelationalMigrationThread();

        String userDir = System.getProperty("user.dir");

        DataWanderPresenterMock presenterMock = new DataWanderPresenterMock();
        DataWanderViewIntMock viewMock = new DataWanderViewIntMock();
        ConnectionValues connValues = new ConnectionValues();

        connValues.setObjectCompiledFolder(userDir + File.separator + "testCompiledCode" +
                File.separator + dir);
        System.out.println("generatePathToDb4o(userDir) "+generatePathToDb4o(userDir));
        connValues.setObjectFilename(generatePathToDb4o(userDir));

        connValues.setRelationalUsername(databaseUser);
        connValues.setRelationalPassword("123");
        connValues.setRelationalURL("jdbc:oracle:thin:@localhost:1521:xe");

        instance.setDataWanderPresenter(presenterMock);
        instance.setDataWanderView(viewMock);
        instance.setConnectionValues(connValues);

        // then
        instance.migrationProcess();

        // when
        //fail("The test case is a prototype.");
    }

    private String generatePathToDb4o(String path) {
        //F:\Java_programy\DataWander_google\testCompiledCode\obj2relTest01\
        //F:\Java_programy\DataWander_google\testCompiledCode\obj2relTest01\obj2relTest.yap
        return path + File.separator + //"test" + File.separator + "1111test.yap";
                "testCompiledCode" + File.separator +
                dir + File.separator + objectDBFilename;
    }

    private void dropAllTables() throws MyRelationalDataBaseException, SQLException {
        OracleConnector.setUsername(databaseUser);
        OracleConnector.setPassword("123");
        OracleConnector.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        OracleConnector.initDatabaseDriver();

        String sql = "select DISTINCT user_objects.OBJECT_NAME from " +
                "(user_objects LEFT OUTER JOIN  recyclebin ON user_objects.OBJECT_NAME = recyclebin.OBJECT_NAME) " +
                "where user_objects.object_type = 'TABLE' and recyclebin.OBJECT_NAME is null";
        Connection conn = OracleConnector.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.execute();
        ResultSet rs = stmt.getResultSet();

        while(rs.next()) {
            String tableName = rs.getString("OBJECT_NAME");
            System.out.println("Drop: " + tableName);
            stmt = conn.prepareStatement("drop table \"" + tableName + "\" cascade constraints ");
            stmt.execute();
        }
    }
}
